#include "../cpp-httplib/httplib.h"
#include "oj_control.hpp"
using namespace httplib;
using namespace ns_control;
int main()
{
      Server s;
      control con;
      // 获取题目列表
      s.Get("/all_questions", [&con](const Request &req, Response &rsp)
      { 
          std::string html;
          con.allquestions(&html);
          //用户看到的是什么呢？？网页数据 + 拼上了题目相关的数据
          rsp.set_content(html, "text/html; charset=utf-8"); 
      });
      // 获取对应编号的题目
      s.Get(R"(/question/(\d+))", [&con](const Request &req, Response &rsp)
      {
            std::string html;
            const std::string num=req.matches[1];
            con.onequestion(num,&html);
            rsp.set_content(html, "text/html; charset=utf-8"); 
      });
      // 使用判题功能
      s.Post(R"(/judge/(\d+))", [&con](const Request &req, Response &rsp)
      {
            std::string html;
            std::string result_json;
            const std::string num=req.matches[1];
            con.judge(num,req.body,&result_json);
            rsp.set_content(result_json, "application/json;charset=utf-8");
      });
      s.set_base_dir("./wwwroot");
      s.listen("0.0.0.0", 8081);
      return 0;
}